package com.itbaizhan.shopping_admin_service.service;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itbaizhan.shopping_admin_service.mapper.RoleMapper;
import com.itbaizhan.shopping_common.pojo.Role;
import com.itbaizhan.shopping_common.service.RoleService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@DubboService
public class RoleServiceImpl implements RoleService {

   @Autowired
   private RoleMapper roleMapper;
    //添加角色
    @Override
    public void add(Role role) {
       roleMapper.insert(role);
    }
    //修改角色
    @Override
    public void update(Role role) {
     roleMapper.updateById(role);
    }
  ///删除角色
    @Override
    public void delete(Long id) {
     //删除角色
      roleMapper.deleteById(id);
      //删除角色对应的权限信息
        roleMapper.deletePermissionToRole(id);
        //删除角色对应的管理员信息
        roleMapper.deleteRoleAllAdmin(id);
    }

    /**
     * 查询角色的信息 对应的权限
     * @param rid
     * @return
     */
    @Override
    public Role findById(Long rid) {
        Role byId = roleMapper.findById(rid);
        return byId;
    }

    /**
     * 分页查询角色信息
     * @param page
     * @param size
     * @return
     */
    @Override
    public Page<Role> search(int page, int size) {
        return roleMapper.selectPage(new Page<Role>(page,size),null);
    }

    /**
     * 修改角色的权限信息
     * @param rid
     * @param pids
     */
    @Override
    public void updatePermissionToRole(Long rid, Long[] pids) {
       //删除角色对应的权限信息
        roleMapper.deletePermissionToRole(rid);
        //给角色添加权限
        for (Long pid : pids) {
            roleMapper.addPermissionToARole(rid,pid);
        }
    }

    @Override
    public List<Role> findAll() {
        return roleMapper.selectList(null);
    }
}
